package cn.gok.homework;

import java.util.*;
import java.util.stream.Stream;


class Book implements Comparable<Book>{
    private int bid;
    private String bname;
    private float price;

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname=" + bname +
                ", price=" + price +
                '}';
    }
//按照书的价格从大到小排序
    @Override
    public int compareTo(Book o) {
        if (this.getPrice()>o.getPrice()){
            return -1;
        }else if (this.getPrice()==o.getPrice()&&this.getBname().equals(o.getBname())){
            return 0;
        }
        return 1;
    }
}
public class HomeWork {
    public static void main(String[] args) {
        //test2();
        test3();
    }

    private static void test3() {
//        3  创建   ArrayList<Book> list;添加5本书，
//        使用 stream运算，将价格低于150元的书，按编号从低到高输出。
//        提示    filter(?).sort(new BookCompartor()).forEach(...)

        List<Book> list = Arrays.asList(new Book(1, "西游记", 100),
                new Book(2,"战神记",140),
                new Book(3,"蠢驴记",90),
                new Book(3,"蠢驴记",80),
                new Book(5,"giao记",300));
        Stream<Book> stream = list.stream();
        stream.filter(x->x.getPrice()<150).sorted(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getBid()<o2.getBid()){
                    return -1;
                }else if (o1.getBid()==o2.getBid()&&o1.getBname().equals(o2.getBname())){
                    return 0;
                }
                return 1;
            }
        }).forEach(System.out::println);
    }

    private static void test2() {
//        创建 TreeMap<Book,String> map;加入5本书，
//        map.put(new Book(1,"西游记",100),"老白");
        TreeMap<Book, String> map = new TreeMap<>();
        map.put(new Book(1,"西游记",100),"老白");
        map.put(new Book(2,"战神记",200),"hh");
        map.put(new Book(3,"蠢驴记",300),"ee");
        map.put(new Book(4,"蠢驴记",250),"dd");
        map.put(new Book(5,"giao记",300),"mm");
        //要求按书的价格从高到低排序输出。
        //使用两个遍历方法：  keySet遍历，entrySet遍历
        for (Book book : map.keySet()) {
            System.out.println(book);
        }
        System.out.println("-----");
        Iterator<Map.Entry<Book, String>> it = map.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<Book, String> result = it.next();
            System.out.println(result.getKey());
        }
    }
}
